import {
  Body,
  Controller,
  Delete,
  Get,
  HttpException,
  HttpStatus,
  Param,
  Patch,
  Post,
} from '@nestjs/common';

@Controller('posts')
export class PostsController {
  @Post()
  createPost(@Body() data: any) {
    console.log(data);
    throw new HttpException('后端主动抛出错误', HttpStatus.OK);
    // return data;
  }

  @Delete(':id')
  async deleteById(@Param() id: number) {
    console.log(id);
    return '删除成功';
  }

  @Patch(':id')
  async modifyById(@Param() id: number, data: any) {
    console.log(id, data);
    return '修改成功';
  }

  @Get()
  async postsLists() {
    throw new HttpException('后端主动抛出错误', HttpStatus.OK);
    // return [
    //   {
    //     id: 1,
    //     name: '张三23',
    //   },
    //   {
    //     id: 2,
    //     name: '张三1',
    //   },
    // ];
  }

  @Get(':id')
  async posts(@Param() id: number) {
    console.log(id);
    return {
      id: 1,
      name: '张三',
    };
  }
}
